package com.vpen.utils;

import com.vpen.model.resp.LicensePart;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.openssl.PEMKeyPair;
import org.bouncycastle.openssl.PEMParser;
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;

import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.*;
import java.util.Base64;

public class SignatureExample {
    private static String licensePart = "{\"licenseId\":\"FV8EM46DQYC5AW9\",\"licenseeName\":\"Vpen\",\"licenseeType\":\"PERSONAL\",\"assigneeName\":\"\",\"assigneeEmail\":\"\",\"licenseRestriction\":\"\",\"checkConcurrentUse\":false,\"products\":[{\"code\":\"GO\",\"fallbackDate\":\"2026-09-14\",\"paidUpTo\":\"2026-09-14\",\"extended\":false},{\"code\":\"RS0\",\"fallbackDate\":\"2026-09-14\",\"paidUpTo\":\"2026-09-14\",\"extended\":false},{\"code\":\"DM\",\"fallbackDate\":\"2026-09-14\",\"paidUpTo\":\"2026-09-14\",\"extended\":false},{\"code\":\"CL\",\"fallbackDate\":\"2026-09-14\",\"paidUpTo\":\"2026-09-14\",\"extended\":false},{\"code\":\"RSU\",\"fallbackDate\":\"2026-09-14\",\"paidUpTo\":\"2026-09-14\",\"extended\":false},{\"code\":\"RSC\",\"fallbackDate\":\"2026-09-14\",\"paidUpTo\":\"2026-09-14\",\"extended\":true},{\"code\":\"PC\",\"fallbackDate\":\"2026-09-14\",\"paidUpTo\":\"2026-09-14\",\"extended\":false},{\"code\":\"DS\",\"fallbackDate\":\"2026-09-14\",\"paidUpTo\":\"2026-09-14\",\"extended\":false},{\"code\":\"RD\",\"fallbackDate\":\"2026-09-14\",\"paidUpTo\":\"2026-09-14\",\"extended\":false},{\"code\":\"RC\",\"fallbackDate\":\"2026-09-14\",\"paidUpTo\":\"2026-09-14\",\"extended\":false},{\"code\":\"RSF\",\"fallbackDate\":\"2026-09-14\",\"paidUpTo\":\"2026-09-14\",\"extended\":true},{\"code\":\"RM\",\"fallbackDate\":\"2026-09-14\",\"paidUpTo\":\"2026-09-14\",\"extended\":false},{\"code\":\"II\",\"fallbackDate\":\"2026-09-14\",\"paidUpTo\":\"2026-09-14\",\"extended\":false},{\"code\":\"DPN\",\"fallbackDate\":\"2026-09-14\",\"paidUpTo\":\"2026-09-14\",\"extended\":false},{\"code\":\"DB\",\"fallbackDate\":\"2026-09-14\",\"paidUpTo\":\"2026-09-14\",\"extended\":false},{\"code\":\"DC\",\"fallbackDate\":\"2026-09-14\",\"paidUpTo\":\"2026-09-14\",\"extended\":false},{\"code\":\"PS\",\"fallbackDate\":\"2026-09-14\",\"paidUpTo\":\"2026-09-14\",\"extended\":false},{\"code\":\"RSV\",\"fallbackDate\":\"2026-09-14\",\"paidUpTo\":\"2026-09-14\",\"extended\":true},{\"code\":\"WS\",\"fallbackDate\":\"2026-09-14\",\"paidUpTo\":\"2026-09-14\",\"extended\":false},{\"code\":\"PSI\",\"fallbackDate\":\"2026-09-14\",\"paidUpTo\":\"2026-09-14\",\"extended\":true},{\"code\":\"PCWMP\",\"fallbackDate\":\"2026-09-14\",\"paidUpTo\":\"2026-09-14\",\"extended\":true},{\"code\":\"RS\",\"fallbackDate\":\"2026-09-14\",\"paidUpTo\":\"2026-09-14\",\"extended\":true},{\"code\":\"DP\",\"fallbackDate\":\"2026-09-14\",\"paidUpTo\":\"2026-09-14\",\"extended\":true},{\"code\":\"PDB\",\"fallbackDate\":\"2026-09-14\",\"paidUpTo\":\"2026-09-14\",\"extended\":true},{\"code\":\"PRR\",\"fallbackDate\":\"2026-09-14\",\"paidUpTo\":\"2026-09-14\",\"extended\":true}],\"metadata\":\"0220240702PSAX000005X\",\"hash\":\"12345678/0-541816629\",\"gracePeriodDays\":7,\"autoProlongated\":false,\"isAutoProlongated\":false,\"trial\":false,\"aiAllowed\":true}";
    public static void main(String[] args) throws Exception {
//        //加载私钥PKCS#1格式的私钥 -----BEGIN RSA PRIVATE KEY----- 开头的
//        PrivateKey privateKey = loadPKCS1PrivateKey("ca.key");
//
//        byte[] sign = signParam(privateKey, licensePart);
//        String encodedSignature = Base64.getEncoder().encodeToString(sign);
//        System.out.println(encodedSignature);

        String json = LicensePart.buildJson("Vpen98", "2030-01-01");
        System.out.println(json);


    }

    private static byte[] signParam(PrivateKey privateKey,String data){
        try {
            // 创建Signature对象并初始化
            Signature signature = Signature.getInstance("SHA1withRSA");
            signature.initSign(privateKey);

            // 更新要签名的数据
            signature.update(data.getBytes(StandardCharsets.UTF_8));

            // 生成签名
            return signature.sign();
        } catch (NoSuchAlgorithmException | InvalidKeyException | SignatureException e) {
            throw new RuntimeException(e);
        }
    }


    /**
     * 加载私钥
     * @Author Vpen
     **/
    private static PrivateKey loadPKCS1PrivateKey(String fileName){
        try {
            // 加载BouncyCastleProvider
            Security.addProvider(new BouncyCastleProvider());
            // 加载BouncyCastleProvider
            PEMParser pemParser = new PEMParser(new FileReader(fileName));
            JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("BC");
            Object object = pemParser.readObject();
            KeyPair kp = converter.getKeyPair((PEMKeyPair) object);
            return kp.getPrivate();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}